
import {ACCESS_TOKEN} from '@/config/storage.js'
import {setUrlParams} from '@/utils/index.js'
import {removeLoginInfo} from '@/utils/storage.js'
import {toUserPage, toUserLoginPage} from '@/utils/PageRouter.js'

export default {
	//	公共请求入口
	request(options) {
		const {url} = options
		const token = uni.getStorageSync(ACCESS_TOKEN)
		if (token) {
			options.header = {
				'x-api-key': token
			}
		}
		// isCatch
		return new Promise((resolve, reject) => {
			uni.request({
				...options,
				success: function (res) {
					try{
						if (res.statusCode === 200) {
							switch(res.data.code) {
								case 200:
									resolve(res.data)
									break
								case 401:
									removeLoginInfo()
									// #ifndef APP-PLUS
									uni.showToast({title: '请先登陆才能使用此功能', icon: 'none', duration: 3000})
									// #endif
									// setTimeout(() => {
										toUserPage(true)
										reject(res.data)
									// }, 1000)
									break
								case 404:
									uni.hideLoading()
									removeLoginInfo()
									getApp().globalData.islogin = false
									uni.showToast({title: '登录已过期，请重新登录', icon: 'none'})
									reject(res.data)
									setTimeout(()=> {
										toUserLoginPage()
									}, 1500)
									break
								case 422:
									uni.hideLoading()
									uni.showToast({title: res.data.message, icon: 'none'})
									reject(res.data)
									break
								case 423:
									uni.hideLoading()
									uni.showToast({title: res.data.message, icon: 'none'})
									reject(res.data)
									break
								default:
									// uni.hideLoading()
									// setTimeout(() => {
									// 	uni.showToast({title: message, icon: 'none'})
									// })
									reject(res.data)
							}
						} else {
							reject(res.data)
						}
					}catch(e){
						reject(res.data)
					}
				},
				fail: res => {
					uni.hideLoading()
					setTimeout(() => {
						uni.showToast({title: '连接超时！', icon: 'none'})
					})
					reject(res.data)
				}
			})
		})
	},
	//	GET 请求入口
	get (options) {
		return this.request({...options})
	},
	//	POST 请求入口
	post (options) {
		return this.request({...options, method: 'POST'})
	},
	//	GET 请求入口
	del (options) {
		if (options.params) {
			options.url = setUrlParams(options.url, options.params)
		}
		return this.request({...options, method: 'DELETE'})
	},
	upload(options, filePath) {
		const header = {}
		const token = uni.getStorageSync(ACCESS_TOKEN)
		if (token) {
			header['x-api-key'] = token
		}
		
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: options.url,
				header,
				filePath: options.filePath,
				fileType: 'image',
				name: 'file',
				success: (res) => {
					const data = JSON.parse(res.data)
					try{
						if (res.statusCode === 200) {
							switch(data.code) {
								case 200:
									resolve(data)
									break
								case 401:
									removeLoginInfo()
									uni.showToast({title: '用户 token 过期, 请重新登录', icon: 'none'})
									setTimeout(() => {
										toUserPage()
										reject(data)
									}, 1000)
									break
								default:
									reject(data)
							}
						} else {
							reject(data)
						}
					}catch(e){
						reject(data)
					}
				},
				fail: (res) => {
					const data = JSON.parse(res.data)
					reject(data)
				}
			})
		})
	}
}
